package profController;

import java.io.IOException;
import java.util.Vector;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BeanQuestions;
import model.BeanQuestionsRepondues;
import model.Question;

/**
 * Servlet implementation class ServletFrontProf
 */
@WebServlet("/ServletFrontProf")
public class ServletFrontProf extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public ServletFrontProf() {
        // TODO Auto-generated constructor stub
    	
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(true);
		String strDispatch = "";
		
		request.setCharacterEncoding("UTF-8");
		
		if(session.getAttribute("monBeanQuestions") == null)
		{
			strDispatch = "ServletHomeProf";
		}
		else
		{
			strDispatch = "prof/choixQuestions.jsp";
		}
		
		RequestDispatcher dispatcher = request.getRequestDispatcher(strDispatch);			
		dispatcher.forward(request,response);	
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		HttpSession session = request.getSession(true);
		ServletContext context = request.getSession().getServletContext();
		
		BeanQuestions monBeanQuestions = (BeanQuestions)session.getAttribute("monBeanQuestions");
		
		BeanQuestions questionsChoisies = new BeanQuestions();
		
		Vector<Question> listeQuestion = monBeanQuestions.getListeQuestion();
		for(int i=0;i<monBeanQuestions.getSize();i++)
		{
			if(request.getParameter("question_"+i) != null)
			{
				questionsChoisies.add(listeQuestion.get(i));
			}
		}
		
		context.setAttribute("questionsChoisies",questionsChoisies);
		
		// gestion resultat plusieurs eleve
		Vector<BeanQuestionsRepondues> ttesLesReponses = new Vector<BeanQuestionsRepondues>();
		context.setAttribute("ttesLesReponses", ttesLesReponses);
		
//		RequestDispatcher dispatcher = request.getRequestDispatcher("prof/homeProf.jsp");	
		response.sendRedirect("ServletHomeProf");			
//		dispatcher.forward(request,response);
	}

}
